package com.hgm.study.viewmodel

import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import com.hgm.study.model.entity.Article
import com.hgm.study.model.entity.Video
import com.hgm.study.model.serice.VideoService
import kotlinx.coroutines.delay

/**
 * 视频列表单独的ViewModel
 * @author    HGM
 * @version   1.0
 */
class VideoViewModel : ViewModel() {
      private val pageSize = 10
      private var pageOffset = 1
      private val videoService = VideoService.instance()
      var refreshing by mutableStateOf(false)
            private set
      var hasMore by mutableStateOf(true)
            private set
      var listLoader by mutableStateOf(false)
            private set

      var list by mutableStateOf(
            listOf(
                  Video(
                        title = "行测老师告诉你如何制定适合自己的学习方案",
                        duration = "00:02:00",
                        imageUrl = "https://docs.bughub.icu/compose/assets/banner1.webp"
                  ),
                  Video(
                        title = "行测老师告诉你如何制定适合自己的学习方案",
                        duration = "00:02:00",
                        imageUrl = "https://docs.bughub.icu/compose/assets/banner1.webp"
                  ),
            )
      )
            private set


      //获取
      suspend fun fetchVideoList() {
            val videoResponse = videoService.getVideo(pageOffset, pageSize)
            if (videoResponse.code == 0) {
                  val tmpList = mutableListOf<Video>()
                  //当不是第一次加载时把上次加载的数据加入进来
                  if (pageOffset != 1) {
                        tmpList.addAll(list)
                  }
                  //获取新数据
                  videoResponse.data?.let { tmpList.addAll(it) }
                  list = tmpList
                  //当返回的数量与页数不相等时说明没有更多数据了
                  hasMore = videoResponse.data?.size == pageSize
                  listLoader = true
                  refreshing = false
            } else {
                  val message = videoResponse.message
                  Log.e("hgm", "VideoData网络加载信息: $message")
                  pageOffset--
                  if (pageOffset < 1) {
                        pageOffset = 1
                  }
            }
      }

      //刷新
      suspend fun refresh() {
            pageOffset = 1//?
            refreshing = true
            fetchVideoList()
      }

      //加载更多
      suspend fun loadMore() {
            if (hasMore) {
                  pageOffset++
                  fetchVideoList()
            }
      }

      var videoUrl by mutableStateOf("")
            private set

      var coverUrl by mutableStateOf("")
            private set

      private var videoTitle by mutableStateOf("习近平@战“疫”一线的这些群体：你们为疫")

      //HTML 头部
      private val htmlHeader = """
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title></title>
            <style>
                img {
                    max-width: 100% !important;
                }
            </style>
        </head>
        <body>
    """.trimIndent()

      //html尾部
      private val htmlFooter = """
        </body>
        </html>
    """.trimIndent()

      var videoDesc by mutableStateOf(
            """$htmlHeader
      <h5 style="color:#333333;font-size:16px;">$videoTitle</h5>
        $htmlFooter
    """.trimIndent()
      )

      var infoLoaded by mutableStateOf(false)
            private set

      private var videoEntity: Video? = null

      suspend fun fetchInfo() {
            val res = videoService.info("")
            if (res.code == 0) {
                  videoEntity = res.data
                  videoTitle = videoEntity!!.title
                  videoUrl = videoEntity!!.video ?: ""
                  coverUrl = videoEntity!!.imageUrl
                  videoDesc = """$htmlHeader
              <h5 style="color:#333333;font-size:16px;">$videoTitle</h5>
              ${videoEntity?.desc}
                $htmlFooter
            """.trimIndent()
            }

            infoLoaded = true
      }
}